package cn.wen.yinxiu.member.controller;

import java.util.ArrayList;
import java.util.List;

import cn.wen.yinxiu.common.convention.result.Results;
import cn.wen.yinxiu.member.service.UsersService;
import cn.wen.yinxiu.member.vo.UserFriend;
import cn.wen.yinxiu.member.vo.UserFriendInfo;
import cn.wen.yinxiu.member.vo.UserFriendQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.wen.yinxiu.member.service.FriendService;
import cn.wen.yinxiu.common.convention.result.ResultEntity;


/**
 * @ClassName: Friend
 * @Author 小飞
 * @Email coderwen22@163.com
 * @Date 2023-05-21 22:19:26
 * @Description: 朋友相关的控制层操作
 */
@RestController
@RequestMapping("/web")
public class FriendController {

    @Autowired
    private FriendService friendService;

    @Autowired
    private UsersService usersService;

    /**
     * 获取当前用户关注的用户
     *
     * @param userId
     * @param page
     * @return
     */
    @GetMapping("/api/user/friend/follow/list")
    public ResultEntity getFriendList(@RequestParam("userId") Long userId,
                                      @RequestParam("page") Integer page) {
        // 1.获取用户的好友列表
        List<UserFriendInfo> friendInfoList = usersService.queryUserFriendInfoList(userId, page);
        return Results.success(friendInfoList);
    }

    /**
     * 通过音秀号或者用户名查询当前用户关注的用户
     *
     * @return
     */
    @GetMapping("/api/user/friend/search/follow/list")
    public ResultEntity searchFriendList(@RequestBody UserFriendQuery query) {
        // 1.获取用户的好友列表
        List<UserFriendInfo> friendInfoList =
                usersService.searchUserFriendInfoList(query);
        return Results.success(friendInfoList);
    }

    /**
     * 获取当前用户粉丝的用户
     *
     * @param userId
     * @param page
     * @return
     */
    @GetMapping("/api/user/friend/fans/list")
    public ResultEntity getFansList(@RequestParam("userId") Long userId,
                                    @RequestParam("page") Integer page) {
        // 1.获取用户的好友列表
        List<UserFriendInfo> friendInfoList = usersService.queryUserFansInfoList(userId, page);
        return Results.success(friendInfoList);
    }

    /**
     * 通过音秀号或者用户名查询当前用户被关注的用户
     *
     * @return
     */
    @GetMapping("/api/user/friend/search/fans/list")
    public ResultEntity searchFansList(@RequestBody UserFriendQuery query) {
        // 1.获取用户的好友列表
        List<UserFriendInfo> friendInfoList =
                usersService.searchUserFansInfoList(query);
        return Results.success(friendInfoList);
    }

    /**
     * 将用户在对方关注里取消（移除该粉丝）
     *
     * @param userId
     * @param fansId
     * @return
     */
    @PostMapping("/api/user/friend/remove/fans/follow")
    public ResultEntity deleteUserFriendFromFans(@RequestParam("userId") Long userId,
                                                 @RequestParam("fansId") Long fansId) {
        // 1.获取用户的好友列表
        boolean res = usersService.deleteUserFriendFromFansRelation(userId, fansId);
        if (!res) return Results.failure();
        return Results.success();
    }

    /**
     * 取关某个用户
     *
     * @param userId
     * @param friendId
     * @return
     */
    @PostMapping("/api/user/friend/cancel/follow")
    public ResultEntity deleteUserFriendRelation(@RequestParam("userId") Long userId,
                                                 @RequestParam("friendId") Long friendId) {
        boolean res = usersService.deleteUserFriendRelation(userId, friendId);
        if (!res) return Results.failure();
        return Results.success();
    }

    /**
     * 关注用户
     * @param userFriend
     * @return
     */
    @PostMapping("/api/user/friend/follow")
    public ResultEntity followUser(@RequestBody UserFriend userFriend) {
        List<UserFriend> list = new ArrayList<>();
        list.add(userFriend);
        usersService.addUserFriend(list);
        return Results.success();
    }


    /**
     * 获取所有的互关对象
     *
     * @param userId
     * @return
     */
    @GetMapping("/api/user/friend/list")
    ResultEntity<List<Long>> getUserFriendList(@RequestParam("userId") Long userId) {
        List<Long> friendList = friendService.queryFriendIdList(userId);
        return Results.success(friendList);
    }


}
